//----------------------------------------------------------------------------
// View 'Kaart'
//----------------------------------------------------------------------------
Ti.include('lib/include.js');
Ti.include('lib/constants.js');
Ti.include('lib/strings.js');

var Map = require('ti.map');
var winMap = Titanium.UI.currentWindow;
winMap.addEventListener('focus', function(e) {
   log("Window 'Kaart' actief");
});
var locations = [];


locations.push(Map.createAnnotation({
   latitude : 50.842496,
   longitude : 5.727246,
   title : T_MAP_ODK_TITLE,
   subtitle : T_MAP_ODK_SUBTITLE,
   leftButton: '/images/icons/hhlogo-30.png',
   pincolor : Map.ANNOTATION_GREEN,
   animate : true,
   id : 'locatieODK' // CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS
}));

locations.push(Map.createAnnotation({
   latitude : 50.838250,
   longitude : 5.73310,
   title : T_MAP_NBS_TITLE,
   subtitle : T_MAP_NBS_SUBTITLE,
   leftButton: '/images/icons/hhlogo-30.png',
   pincolor : Map.ANNOTATION_GREEN,
   animate : true,
   id : 'locatieNBS'
}));


var mapview = Map.createView({
   mapType : Map.STANDARD_TYPE,
   region : {
      latitude : 50.840000,
      longitude : 5.73000,
      latitudeDelta : 0.009,
      longitudeDelta : 0.009
   },
   animate : true,
   regionFit : true,
   userLocation : true,
   enableZoomControls: true,
   userLocationButton: true,
   userLocation: true,
   annotations: locations
});


mapview.addEventListener('click',function(e){
    if (e.clicksource == 'leftButton') {
       if (e.annotation.id == 'locatieODK') {
          log("TODO: Locatie 1 info tonen");
       }
       else if (e.annotation.id == 'locatieNBS') {
          log("TODO: Locatie 2 info tonen");
       }
    }

});


//----- Functions

//Zoom in
var btnZoomIn = Titanium.UI.createButton({
   image:'/images/icons/plus32.png'
});
btnZoomIn.addEventListener('click', function(e) {
   mapview.zoom(1);
});

//Zoom out
var btnZoomOut = Titanium.UI.createButton({
   image:'/images/icons/minus32.png'
});
btnZoomOut.addEventListener('click', function(e) {
   mapview.zoom(-1);
});

//Standaard map type
var btnMapStd = Titanium.UI.createButton({
   title: 'Std'
});
btnMapStd.addEventListener('click', function(e) {
   mapview.setMapType(Map.STANDARD_TYPE);
});

//Hybrid map type
var btnMapHybrid = Titanium.UI.createButton({
   title: 'Hyb'
});
btnMapHybrid.addEventListener('click', function(e) {
   mapview.setMapType(Map.HYBRID_TYPE);
});

//Satelliet map type
var btnMapSat = Titanium.UI.createButton({
   title: 'Sat'
});
btnMapSat.addEventListener('click', function(e) {
   mapview.setMapType(Map.SATELLITE_TYPE);
});

var flexSpace = Titanium.UI.createButton({
   systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});


//----- Toolbar
var mapToolBar = Titanium.UI.iOS.createToolbar({
    items:[btnZoomIn, btnZoomOut, flexSpace, btnMapStd, btnMapHybrid, btnMapSat],
    bottom:0,
    borderTop:false,
    borderBottom:false
}); 
if (isIOS7Plus()) {
   mapToolBar.setOpacity(1.0);
}
else {
   mapToolBar.setBarColor(donkergroen);
   mapToolBar.setColor(wit);   
}


//----- Add UI elements
winMap.add(mapview, mapToolBar);


